<?php
namespace Admin\Model;
use Think\Model;

class TemplateModel extends Model{
	//自动验证
	protected $_validate = array(
		array('name','1,20','模板名称不得为空或大于20位！',self::EXISTS_VALIDATE,'length'),
		array('file','1,20','文件名不得为空或大于20位！',self::EXISTS_VALIDATE,'length'),
		array('file2','/^\w+\.html$/','文件名必须是字母、数字、下划线，且必须以.html结尾！',self::VALUE_VALIDATE),
		array('success','1,255','提交成功的提示信息不得为空或大于255位！',self::EXISTS_VALIDATE,'length'),
		array('success2','1,255','支付成功的提示信息不得为空或大于255位！',self::EXISTS_VALIDATE,'length'),
		array('often','1,255','频繁提交的提示信息不得为空或大于255位！',self::EXISTS_VALIDATE,'length'),
	);
	
	//查询总记录
	public function total(){
		return $this->where($this->map())->count();
	}
	
	//查询所有
	public function all($firstRow){
		return $this->field('id,name,file,uid,template,sid,state,search,send,qq,selected,date')
    				->where($this->map())
    				->order(array('date'=>'DESC'))
    				->limit($firstRow,C('PAGE_SIZE'))
    				->select();
	}
	
	//查询所有（不分页）
	public function all2(){
		return $this->field('id,name,selected')
    				->order(array('date'=>'DESC'))
    				->select();
	}
	
	//查询一条
	public function one($id=0){
		$map['id'] = $id ? $id : I('get.id');
		return $this->field('name,file,uid,template,sid,product,field,pay,state,search,send,verify,qq,success,success2,often,selected')->where($map)->find();
	}
	
	//添加
	public function insert(){
		$data = array(
			'name'=>I('post.name'),
			'file'=>I('post.file'),
			'file2'=>I('post.file'),
			'uid'=>I('post.uid'),
			'template'=>I('post.template'),
			'sid'=>I('post.template')==1 ? 0 : I('post.sid'),
			'field'=>implode(',',I('post.field')),
			'pay'=>I('post.pay') ? I('post.selectedPay').'|'.implode(',',I('post.pay')) : I('post.selectedPay'),
			'state'=>I('post.state'),
			'search'=>I('post.search'),
			'send'=>I('post.template')==1 ? 0 : I('post.send'),
			'qq'=>I('post.qq'),
			'success'=>I('post.success',NULL,'stripslashes'),
			'success2'=>I('post.success2',NULL,'stripslashes'),
			'often'=>I('post.often',NULL,'stripslashes'),
			'date'=>NOW_TIME
		);
		if (I('post.protype') == 0){
			if (!is_array(I('post.pid1'))) return '请至少选择一个产品！';
			$data['product'] = '0|'.I('post.sort1').'|'.implode(',',I('post.pid1')).'|'.I('post.selected1').'|'.I('post.viewtype');
		}elseif (I('post.protype') == 1){
			if (!is_array(I('post.pid2'))) return '请至少选择一个产品！';
			$data['product'] = '1|'.I('post.sort2').'|'.implode(',',I('post.pid2')).'|'.I('post.selected2').'|'.I('post.viewtype');
		}
		if (I('post.verify') == 0){
			$data['verify'] = '0';
		}elseif (I('post.verify') == 1){
			$data['verify'] = '1,'.I('post.verify_lang').','.I('post.verify_width').','.I('post.verify_height').','.I('post.verify_length').','.I('post.verify_fontsize');
		}
		if ($this->create($data)){
			if ($this->repeat()) return '此模板名或文件名已存在！';
			$id = $this->add();
			return $id ? $id : 0;
		}else{
			return $this->getError();
		}
	}
	
	//修改
	public function update(){
	$data = array(
			'id'=>I('get.id'),
			'name'=>I('post.name'),
			'file'=>I('post.file'),
			'file2'=>I('post.file'),
			'uid'=>I('post.uid'),
			'template'=>I('post.template'),
			'sid'=>I('post.template')==1 ? 0 : I('post.sid'),
			'field'=>implode(',',I('post.field')),
			'pay'=>I('post.pay') ? I('post.selectedPay').'|'.implode(',',I('post.pay')) : I('post.selectedPay'),
			'state'=>I('post.state'),
			'search'=>I('post.search'),
			'send'=>I('post.template')==1 ? 0 : I('post.send'),
			'qq'=>I('post.qq'),
			'success'=>I('post.success',NULL,'stripslashes'),
			'success2'=>I('post.success2',NULL,'stripslashes'),
			'often'=>I('post.often',NULL,'stripslashes')
		);
		if (I('post.protype') == 0){
			if (!is_array(I('post.pid1'))) return '请至少选择一个产品！';
			$data['product'] = '0|'.I('post.sort1').'|'.implode(',',I('post.pid1')).'|'.I('post.selected1').'|'.I('post.viewtype');
		}elseif (I('post.protype') == 1){
			if (!is_array(I('post.pid2'))) return '请至少选择一个产品！';
			$data['product'] = '1|'.I('post.sort2').'|'.implode(',',I('post.pid2')).'|'.I('post.selected2').'|'.I('post.viewtype');
		}
		if (I('post.verify') == 0){
			$data['verify'] = '0';
		}elseif (I('post.verify') == 1){
			$data['verify'] = '1,'.I('post.verify_lang').','.I('post.verify_width').','.I('post.verify_height').','.I('post.verify_length').','.I('post.verify_fontsize');
		}
		if ($this->create($data)){
			if ($this->repeat(1)) return '此模板名或文件名已存在！';
			$id = $this->save();
			return $id ? $id : 0;
		}else{
			return $this->getError();
		}
	}
	
	//设置默认
	public function selected(){
		$map = array('selected'=>1);
		$data = array('selected'=>0);
		$this->where($map)->save($data);
		$data = array(
			'id'=>I('get.id'),
			'selected'=>1
		);
		return $this->save($data);
	}
	
	//删除
	public function remove(){
		$affected_rows = $this->delete(I('get.id'));
		if ($affected_rows) $this->execute('OPTIMIZE TABLE `__PREFIX__template`');
		return $affected_rows;
	}
	
	//验证重复
	private function repeat($type=0){
		$map = array(
			'_complex'=>array(
				'name'=>I('post.name'),
				'file'=>I('post.file'),
				'_logic'=>'OR'
			)
		);
		if ($type) $map['id'] = array('NEQ',I('get.id'));
		return $this->field('id')->where($map)->find();
	}
	
	//搜索
	private function map(){
		$map = array(
			'_complex'=>array(
				'name'=>array('like','%'.I('get.keyword').'%'),
				'file'=>array('like','%'.I('get.keyword').'%'),
				'_logic'=>'OR'
			)
		);
		return $map;
	}
}